﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using Microsoft.Xna.Framework;
using FarseerGames.FarseerPhysics.Factories;
using LaughingDog.Managers;

namespace LaughingDog.Components
{
    public class AngleLimitJoint : Joint
    {
        protected FarseerGames.FarseerPhysics.Dynamics.Joints.AngleLimitJoint mAngleLimitJoint;
        protected RigidBody mConnectedBody = null;

        /// <summary>
        /// The second RigidBody this Spring is attached to. The first RigidBody is attached to the GameObject that the Joint is attached to.
        /// </summary>
        public RigidBody ConnectedBody { get { return mConnectedBody; } set { mConnectedBody = value; } }

        /// <summary>
        /// Gets or sets the minimum angle in degrees of the RigidBody.
        /// </summary>
        public Single LowerLimit { get { return MathHelper.ToDegrees(mAngleLimitJoint.LowerLimit); } set { mAngleLimitJoint.LowerLimit = MathHelper.ToRadians(value); } }

        /// <summary>
        /// Gets or sets the minimum angle in radians of the RigidBody.
        /// </summary>
        public Single LowerLimitRadians { get { return mAngleLimitJoint.LowerLimit; } set { mAngleLimitJoint.LowerLimit = value; } }

        /// <summary>
        /// Gets or sets the maximum angle in degrees of the RigidBody.
        /// </summary>
        public Single UpperLimit { get { return MathHelper.ToDegrees(mAngleLimitJoint.UpperLimit); } set { mAngleLimitJoint.UpperLimit = MathHelper.ToRadians(value); } }

        /// <summary>
        /// Gets or sets the maximum angle in radians of the RigidBody.
        /// </summary>
        public Single UpperLimitRadians { get { return mAngleLimitJoint.UpperLimit; } set { mAngleLimitJoint.UpperLimit = value; } }

        /// <summary>
        /// Gets or sets the slop allowed in the angle past the Min/Max limits.
        /// </summary>
        public Single Slop { get { return mAngleLimitJoint.Slop; } set { mAngleLimitJoint.Slop = value; } }

        /// <summary>
        /// Creates an instance of an AngleLimitJoint component.
        /// </summary>
        /// <param name="gameObject">The GameObject this component is attached to.</param>
        public AngleLimitJoint(GameObject gameObject)
            : base(gameObject)
        {
            mAngleLimitJoint = new FarseerGames.FarseerPhysics.Dynamics.Joints.AngleLimitJoint();
            mJoint = mAngleLimitJoint;
        }

        /// <summary>
        /// Create the Joint after setting initial settings such as the ConnectedBody.
        /// </summary>
        public override void Create()
        {
            mAngleLimitJoint = JointFactory.Instance.CreateAngleLimitJoint(PhysicsManager.Instance.Simulator, GameObject.RigidBody.Body, ConnectedBody.Body, LowerLimitRadians, UpperLimitRadians);

            mJoint = mAngleLimitJoint;
        }
    }
}
